﻿using System;
using System.Threading;
using System.Windows.Forms;
using Com.OpenCode.Framework.Core;

namespace Com.OpenCode.Framework.Log
{
    public class LogActivator : IAddinActivator
    {
        public void Start(Addin addin)
        {
            Application.ThreadException += Application_ThreadException;
            LogCollectionServices.Init(addin.FileInfo.DirectoryName);
        }

        public void Stop(Addin addin)
        {
            Application.ThreadException -= Application_ThreadException;
        }

        private void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            LogServices.WriteError(GetAllMessage(e.Exception), e.Exception.GetType());
        }

        private string GetAllMessage(Exception ex)
        {
            string message = ex.Message;
            if (ex.InnerException!=null)
            {
                message += ";" + ex.InnerException.Message;
            }
            return message;
        }

    }
}